layui.use(['jquery','form'], function(){
    var $ = layui.$;
    var form = layui.form;

    //监听提交
    form.on('submit(formDemo)', function(data){
        console.info(JSON.stringify(data.field));
        $.ajax({
            url:$("#contextPath").val() + "/price/compute",
            type:'post',
            data:data.field,
            success:function(data) {
                $("#perProfit").text(data.perProfit);
            },
            error:function() {
                alert("系统异常,请刷新重试");
            }
        });
        return false;
    });

    //根据长宽高获得体积重
    $("#length").on("blur",function(){
        price.getVolumeWeight();
    });
    $("#width").on("blur",function(){
        price.getVolumeWeight();
    });
    $("#height").on("blur",function(){
        price.getVolumeWeight();
        price.getAmazonLogisticsCost();
    });

    $("#reset").on("click",function(){
        $("#volumeWeight").text("");
        $("#amazonLogisticsCost").text("");
        $("#perProfit").text("");

    });

    //定义所有的方法
    var price = {
        //获得体积重
        getVolumeWeight:function() {
            var length = $.trim($("#length").val());
            var width = $.trim($("#width").val());
            var height = $.trim($("#height").val());
            if (length == "" || width == "" || height == "") {
                $("#volumeWeight").text("");
                return false;
            }
            var volumeWeight = (parseFloat(length) * parseFloat(width) * parseFloat(height))/5000;
            $("#volumeWeight").text(fomatFloat(volumeWeight,3));
        },
        getAmazonLogisticsCost: function () {
            var height = $.trim($("#height").val());
            if (height == "") {
                $("#amazonLogisticsCost").text("");
                return false;
            }
            var amazonLogisticsCost = (parseFloat(height) >= 1.7) ? 3.19 : 2.41;
            $("#amazonLogisticsCost").text(amazonLogisticsCost);
        }
    };

});

function fomatFloat(src,pos){
    return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);
}

